﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reptile
{
    
    public partial class Form1 : Form
    {
        public static List<string> link = new List<string>();   //存储搜索结果页面超链接
        public static int cnt = 0;   //电话号码计数
        public Form1()
        {
            InitializeComponent();
        }
       
        private void searchKeyWord_Click(object sender, EventArgs e)
        {
            //验证关键词是否填写
            if (String.IsNullOrEmpty(this.keyWordBox.Text))
            {
                MessageBox.Show("请输入搜索关键词！", "关键词错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            String bingUrl = CombineBingUrl();     //获取bing搜索引擎搜索网页的url格式
            StreamReader sr = new StreamReader(Download.DownloadFile(bingUrl));
            link = GetHyperLinks(sr.ReadToEnd());   //获取链接

            ThreadPool.SetMaxThreads(20, 20);
            for (int i = 0; i < link.Count; i++)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(GetPhone), i);
            }
        }

        private void GetPhone(object arg)
        {
            try
            {
                this.Invoke(new Action(() =>
                {
                    if (cnt >= 100)   //已爬取100个号码则返回
                        return;
                    bool isInclude = false;   //爬取的电话号码是否有部分由当前线程自其对应的url爬取
                    int i = (int)arg;
                    string url = link[i];
                    string number = "";   //存储当前url爬取的电话号码

                    StreamReader sr = new StreamReader(Download.DownloadFile(url));
                    MatchCollection collection = Regex.Matches(sr.ReadToEnd(), @"1{1}[0-9]{10}");
                    if (collection.Count > 0)
                    {
                        foreach (Match match in collection)
                        {
                            if (cnt >= 100)
                                break;
                            number += (match.ToString() + "   ");
                            cnt++;
                            isInclude = true;
                        }
                        if (isInclude)
                        {
                            //显示电话号码与url
                            phoneBox.Text += number;
                            urlBox.Text += url + "  ";
                        }
                    }

                }));
            }
            catch (Exception)
            {
                MessageBox.Show("error");
            }

        }

        private String CombineBingUrl()   //获取带用户自定义输入的关键字的url（bing搜索引擎的网页的格式）
        {
            String queryWord = UrlEncode(this.keyWordBox.Text);
            String Url = String.Format("https://cn.bing.com/search?" + "q= {0}", queryWord);
            return Url;
        }

        private String UrlEncode(String str)    //实现对关键字编码的功能
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }

            return (sb.ToString());
        }

        private void phoneBox_TextChanged(object sender, EventArgs e)  
        {
            this.phoneBox.Select(this.phoneBox.TextLength, 0);
            this.phoneBox.ScrollToCaret();
        }

        // 提取HTML代码中的网址
        public static List<string> GetHyperLinks(string htmlCode)
        {
            List<string> href = new List<string>();
            //使用正则表达式匹配超链接网址
            string strRegex = @"href=""http(s)*://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
            Regex r = new Regex(strRegex, RegexOptions.IgnoreCase);
            MatchCollection matches = r.Matches(htmlCode);

            bool rep = false;
            foreach (Match match in matches)
            {
                rep = false;

                //提取超链接网址
                string strNew = match.ToString().Substring(6);

                // 过滤重复的URL
                foreach (string str in href)
                {
                    if (strNew == str)
                    {
                        rep = true;
                        break;
                    }
                }

                if (!rep && !strNew.Contains("LinkID") && !strNew.Contains("LinkId") && !strNew.Contains("id=")) href.Add(strNew);
            }

            href.Sort();

            return href;
        }       
    }
}

